package permission

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/status/bizadmin"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/adminrole"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"
)

type AdminRoleDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminRoleDao(ctx context.Context, svcCtx *svc.ServiceContext) *AdminRoleDao {
	return &AdminRoleDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// AdminRoleCreate - 创建后台角色
func (l *AdminRoleDao) AdminRoleCreate(adminRole *ent.AdminRole, tx *ent.Tx) error {
	err := tx.AdminRole.Create().
		SetAdminRoleName(adminRole.AdminRoleName).
		SetAdminRoleRouterIds(adminRole.AdminRoleRouterIds).
		SetAdminRoleDesc(adminRole.AdminRoleDesc).
		SetAdminRoleType(adminRole.AdminRoleType).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminRoleUpdate - 更新后台角色
func (l *AdminRoleDao) AdminRoleUpdate(adminRole *ent.AdminRole, tx *ent.Tx) error {
	ormClient := l.svcCtx.Orm.AdminRole
	if tx != nil {
		ormClient = tx.AdminRole
	}

	err := ormClient.UpdateOneID(adminRole.ID).
		SetAdminRoleName(adminRole.AdminRoleName).
		SetAdminRoleRouterIds(adminRole.AdminRoleRouterIds).
		SetAdminRoleDesc(adminRole.AdminRoleDesc).
		SetAdminRoleType(adminRole.AdminRoleType).
		SetUpdateAt(time.Now()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminRoleDelete - 删除后台角色
func (l *AdminRoleDao) AdminRoleDelete(conditions []predicate.AdminRole) error {
	err := l.svcCtx.Orm.AdminRole.Update().
		Where(conditions...).
		SetIsDelete(bizadmin.Delete).
		SetDeleteAt(time.Now()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminRoleConditionsPageAll - 查询后台角色列表 支持链接名，备注 相似查询
func (l *AdminRoleDao) AdminRoleConditionsPageAll(req *types.AdminRoleListRequest, conditions []predicate.AdminRole) ([]*ent.AdminRole, int, error) {
	offset := common.Offset(req.Current, req.PageSize)

	adminUsers, err := l.svcCtx.Orm.AdminRole.Query().
		Where(conditions...).
		Where(adminrole.IsDeleteEQ(bizadmin.NotDelete)).
		Offset(int(offset)).
		Limit(int(req.PageSize)).
		All(l.ctx)

	if err != nil {
		return nil, 0, err
	}

	total, err := l.svcCtx.Orm.AdminRole.Query().
		Where(conditions...).
		Where(adminrole.IsDeleteEQ(bizadmin.NotDelete)).
		Count(l.ctx)

	return adminUsers, total, nil
}

// AdminRoleConditionsAll - 条件查询后台角色
func (l *AdminRoleDao) AdminRoleConditionsAll(conditions []predicate.AdminRole) ([]*ent.AdminRole, error) {
	adminUsers, err := l.svcCtx.Orm.AdminRole.Query().
		Where(conditions...).
		Where(adminrole.IsDeleteEQ(bizadmin.NotDelete)).
		All(l.ctx)

	if err != nil {
		return nil, err
	}
	return adminUsers, nil
}

// AdminRoleFirst - 条件查询单个后台角色
func (l *AdminRoleDao) AdminRoleFirst(conditions []predicate.AdminRole) (*ent.AdminRole, error) {
	adminRole, err := l.svcCtx.Orm.AdminRole.Query().
		Where(conditions...).
		Where(adminrole.IsDeleteEQ(bizadmin.NotDelete)).
		First(l.ctx)

	if err != nil {
		return nil, err
	}
	return adminRole, nil
}
